﻿using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

	public GameObject joystickMovement;
	public GameObject joystickRotation;
	public CharacterController controller;

	UIJoyStick joyMove;
	UIJoyStick joyRotate;

	public float speed = 1000;
	private Vector3 movement;
	private Vector3 velocity;
	private Transform thisTransform;
	private bool loaded = false;

	void OnLevelWasLoaded(int level)
	{
		if (level == 1)
			loaded = true;
	}
	
	// Update is called once per frame
	void Update () {
		if (loaded) {
			loaded =false;
			joystickMovement = GameObject.Find ("Movement");
			joystickRotation = GameObject.Find ("Rotation");
			if (joystickMovement != null && controller != null && joystickRotation != null) {
				joyMove = joystickMovement.GetComponent<UIJoyStick> ();
				joyRotate = joystickRotation.GetComponent<UIJoyStick> ();
			}
			transform.position = new Vector3(0,-9,0);
		}

		if (joyMove != null && joyRotate != null) {
			transform.Rotate(0.0F, joyRotate.joyStickPosX,0.0F);
			// Apply movement from move joystick
			movement = new Vector3(joyMove.joyStickPosX,0.0F,joyMove.joyStickPosY);
			movement *= Time.deltaTime;
			//movement.Normalize ();

			// Actually move the character 
			controller.SimpleMove( movement*speed);
		}
	}
}
